using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Common.NotifyEvent;
using Controls;
using Report;
using SProject.Paramter;

namespace ProjectView.ProjectControls
{
	public partial class UCProjectDetection : UCBaseOption, IComponentConnector
	{
		public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UCProjectDetection));

		public static readonly DependencyProperty BasicOptionProperty = DependencyProperty.Register("BasicOption", typeof(BasicOption), typeof(UCProjectDetection), new UIPropertyMetadata(null, OnBasicOptionChanged));

		private CollectionViewSource view = new CollectionViewSource();

		public DataGridEx GridControl
		{
			get
			{
				return grid;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return (bool)GetValue(IsReadOnlyProperty);
			}
			set
			{
				SetValue(IsReadOnlyProperty, value);
			}
		}

		public BasicOption BasicOption
		{
			get
			{
				return (BasicOption)GetValue(BasicOptionProperty);
			}
			set
			{
				SetValue(BasicOptionProperty, value);
			}
		}

		private static void OnBasicOptionChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCProjectDetection uCProjectDetection = sender as UCProjectDetection;
			if (uCProjectDetection != null)
			{
				uCProjectDetection.RefreshData();
			}
		}

		public UCProjectDetection()
		{
			InitializeComponent();
			grid.addMenu(new Separator());
			System.Windows.Controls.MenuItem menuItem = new System.Windows.Controls.MenuItem();
			menuItem.Header = Utility.GetMessage("Project_Memu_Data_Export");
			menuItem.Click += MenuExportData_Click;
			grid.IsLineCopy_Paste_Visable = true;
			grid.IsContextMenuVisible = true;
			grid.addMenu(menuItem);
			AddHandler(DataGridEx.OnCopyRowRoutedEvent, new RoutedEventHandler(OnCopyRowRouted_RoutedEvent), true);
		}

		private void OnCopyRowRouted_RoutedEvent(object sender, RoutedEventArgs e)
		{
			CopyRowRoutedEventArgs copyRowRoutedEventArgs = e as CopyRowRoutedEventArgs;
			if (copyRowRoutedEventArgs != null)
			{
				BasicOptionItem basicOptionItem = copyRowRoutedEventArgs.SourceItem as BasicOptionItem;
				BasicOptionItem basicOptionItem2 = copyRowRoutedEventArgs.TargetItem as BasicOptionItem;
				if (basicOptionItem != null && basicOptionItem2 != null)
				{
					basicOptionItem2.DetectionMin = basicOptionItem.DetectionMin;
					basicOptionItem2.DetectionMax = basicOptionItem.DetectionMax;
					e.Handled = true;
				}
			}
		}

		private void grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (IsReadOnly)
			{
				e.Cancel = true;
			}
		}

		private void RefreshData()
		{
			if (BasicOption == null)
			{
				return;
			}
			view.Source = BasicOption.Items;
			view.Filter += view_Filter;
			grid.ItemsSource = view.View;
			foreach (BasicOptionItem item in BasicOption.Items)
			{
				item.Event += BasicOptionItem_Event;
			}
			BasicOption.Items.CollectionChanged += Items_CollectionChanged;
		}

		private void view_Filter(object sender, FilterEventArgs e)
		{
			BasicOptionItem basicOptionItem = e.Item as BasicOptionItem;
			e.Accepted = basicOptionItem.AnalysisType == EAnalysisType.ANALYSIS_QUANTITATIVE;
		}

		private void BasicOptionItem_Event(object sender, EventArgs e)
		{
			PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
			if (propertyChangedEventArgsEx != null && propertyChangedEventArgsEx.PropertyName == "AnalysisType")
			{
				view.View.Refresh();
			}
		}

		private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				foreach (BasicOptionItem newItem in e.NewItems)
				{
					newItem.Event += BasicOptionItem_Event;
				}
			}
			view.View.Refresh();
		}

		public void UnSelectAll()
		{
			grid.UnselectAllCells();
		}

		public bool Validate(bool showMsg = true)
		{
			grid.EndEdit();
			bool flag = true;
			DataGridColumn dataGridColumn = null;
			int index = -1;
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < BasicOption.Items.Count; i++)
			{
				BasicOptionItem basicOptionItem = BasicOption.Items[i];
				if (basicOptionItem.AnalysisType != EAnalysisType.ANALYSIS_QUALITATIVE)
				{
					index = i;
					if (basicOptionItem.DetectionMin < 0.0)
					{
						stringBuilder.Clear();
						stringBuilder.AppendFormat(Utility.GetMessage("msg_prj_LowLimit_should_greater_than"), 0.0.ToString("0.#####"));
						dataGridColumn = ColumnDetectionMin;
						flag = false;
						break;
					}
					if (basicOptionItem.DetectionMax < 0.0)
					{
						stringBuilder.Clear();
						stringBuilder.AppendFormat(Utility.GetMessage("msg_prj_UpLimit_should_greater_than"), 0.0.ToString("0.#####"));
						dataGridColumn = ColumnDetectionMax;
						flag = false;
						break;
					}
					if (basicOptionItem.DetectionMin >= basicOptionItem.DetectionMax)
					{
						stringBuilder.Clear();
						stringBuilder.Append(Utility.GetMessage("msg_prj_LowLimit_should_less_than_prj_UpLimit"));
						dataGridColumn = ColumnDetectionMin;
						flag = false;
						break;
					}
				}
			}
			if (showMsg && !flag && dataGridColumn != null && stringBuilder.Length > 0)
			{
				grid.UnselectAllCells();
				System.Windows.MessageBox.Show(stringBuilder.ToString());
				grid.CurrentColumn = dataGridColumn;
				grid.CurrentItem = BasicOption.Items[index];
				grid.BeginEdit();
			}
			return flag;
		}

		private void MenuExportData_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Title = Utility.GetMessage("Save");
			saveFileDialog.Filter = string.Format("{1}{0}|*.{2}|{3}{0}|*.{3}|{4}{0}|*.{4}", Utility.GetMessage("File"), "Excel", "xlsx", "csv", "txt");
			saveFileDialog.FilterIndex = 1;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				base.Cursor = System.Windows.Input.Cursors.Wait;
				switch (saveFileDialog.FilterIndex)
				{
				case 1:
					SaveToExcel(saveFileDialog.FileName);
					break;
				case 2:
				case 3:
					SaveToCsv(saveFileDialog.FileName);
					break;
				}
				base.Cursor = System.Windows.Input.Cursors.Arrow;
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_data_export_sussess"));
			}
		}

		public void SaveToExcel(string file)
		{
			ExcelData excelData = new ExcelData();
			ExcelSheetData excelSheetData = new ExcelSheetData();
			excelSheetData.Name = Utility.GetMessage("Prj_QuantitativeRange");
			excelSheetData.Data = GetData(true);
			excelData.SheetDataList.Add(excelSheetData);
			ExcelOperator.SaveToExcel(file, excelData);
		}

		public void SaveToCsv(string file)
		{
			CsvTxtOperator.SaveToCsv(file, GetData());
		}

		private List<List<string>> GetData(bool includeMergerCellInfo = false)
		{
			List<List<string>> list = new List<List<string>>();
			List<string> list2 = new List<string>();
			foreach (DataGridColumn column in grid.Columns)
			{
				if (column.Visibility == Visibility.Visible)
				{
					list2.Add(column.Header.ToString());
				}
			}
			list.Add(list2);
			List<string> item = new List<string>();
			foreach (BasicOptionItem item2 in (IEnumerable)grid.Items)
			{
				list2 = new List<string>();
				list2.Add(item2.TargetName);
				list2.Add(item2.DetectionMin.ToString("0"));
				list2.Add(item2.DetectionMax.ToString("0"));
				list.Add(list2);
			}
			if (includeMergerCellInfo)
			{
				list.Insert(0, item);
			}
			return list;
		}
	}
}
